package com.zzjson;

import java.nio.Buffer;
import java.nio.ByteBuffer;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2020 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2020年02月25日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class BufferSlice {
	public static void main(String[] args) {
		ByteBuffer byteBuffer = ByteBuffer.allocate(10);
		for (int i = 0; i < byteBuffer.capacity(); i++) {
			byteBuffer.put((byte) i);
		}

		byteBuffer.position(3);
		byteBuffer.limit(6);
		ByteBuffer slice = byteBuffer.slice();

		output(" slice", slice);
		// slice.flip();
		for (int i = 0; i < slice.capacity(); i++) {
			slice.put((byte) (i * 10));
		}


		byteBuffer.position(0);
		byteBuffer.limit(byteBuffer.capacity());
		while (byteBuffer.remaining() > 0) {
			System.out.println(byteBuffer.get());
		}
	}

	public static void output(String step, Buffer buffer) {
		System.out.println(step + " : ");
		System.out.print("capacity: " + buffer.capacity() + ", ");
		System.out.print("position: " + buffer.position() + ", ");
		System.out.println("limit: " + buffer.limit());
		System.out.println();
	}
}
